package com.njmaomao.gj.pyot.service;

import com.njmaomao.gj.pyot.common.Utils;
import com.njmaomao.gj.pyot.common.bean.dto.*;
import com.njmaomao.gj.pyot.domain.wechat.WechatPayAppRequest;
import com.njmaomao.gj.pyot.domain.wechat.WechatPayJsRequest;
import com.njmaomao.gj.pyot.domain.wechat.wechatapi.orderquery.OrderQueryResp;
import com.njmaomao.gj.pyot.domain.wechat.wechatapi.refund.RefundResp;
import com.njmaomao.gj.pyot.domain.wechat.wechatapi.refundquery.RefundQueryResp;
import com.njmaomao.gj.pyot.domain.model.PayRecord;

import java.util.Arrays;
import java.util.List;

/**
 * DTO<-->Domain装配、转换
 *
 * @author hh 2017年4月16日
 */
public class Assembler {

    public static OrderQueryRespDTO orderQueryRespDTO(OrderQueryResp orderQueryResp) {
        OrderQueryRespDTO orderQueryRespDTO = new OrderQueryRespDTO();
        orderQueryRespDTO.setAppId(orderQueryResp.getAppid());
        orderQueryRespDTO.setBankType(orderQueryResp.getBankType());
        orderQueryRespDTO.setCashFee(orderQueryResp.getCashFee());
        orderQueryRespDTO.setCashFeeType(orderQueryResp.getCashFeeType());
        orderQueryRespDTO.setFeeType(orderQueryResp.getFeeType());
        orderQueryRespDTO.setIsSubscribe(orderQueryResp.getIsSubscribe());
        orderQueryRespDTO.setOpenId(orderQueryResp.getOpenid());
        orderQueryRespDTO.setOrderCode(orderQueryResp.getOutTradeNo());
        orderQueryRespDTO.setTimeEnd(orderQueryResp.getTimeEnd());
        orderQueryRespDTO.setTotalFee(orderQueryResp.getTotalFee());
        orderQueryRespDTO.setTradeState(orderQueryResp.getTradeState());
        orderQueryRespDTO.setTradeStateDesc(orderQueryResp.getTradeStateDesc());
        orderQueryRespDTO.setTradeType(orderQueryResp.getTradeType());
        orderQueryRespDTO.setTransactionId(orderQueryResp.getTransactionId());
        return orderQueryRespDTO;
    }

    public static RefundRespDTO refundRespDTO(RefundResp refundResp) {
        RefundRespDTO refundRespDTO = new RefundRespDTO();
        refundRespDTO.setAppId(refundResp.getAppid());
        refundRespDTO.setOrderCode(refundResp.getOutTradeNo());
        refundRespDTO.setRefundFee(refundResp.getRefundFee());
        refundRespDTO.setRefundId(refundResp.getRefundId());
        refundRespDTO.setRefundOrderCode(refundResp.getOutRefundNo());
        refundRespDTO.setTransactionId(refundResp.getTransactionId());
        return refundRespDTO;
    }

    public static RefundQueryRespDTO refundQueryRespDTO(RefundQueryResp refundQueryResp) {

        RefundQueryRespDTO refundQueryRespDTO = new RefundQueryRespDTO();
        refundQueryRespDTO.setOrderCode(refundQueryResp.getOutTradeNo());
        refundQueryRespDTO.setTransactionId(refundQueryResp.getTransactionId());

        refundQueryRespDTO.setRefundUnits(Arrays.asList(refundUnitDTO(refundQueryResp)));

        return refundQueryRespDTO;
    }

    public static RefundUnitDTO refundUnitDTO(RefundQueryResp refundQueryResp) {
        RefundUnitDTO refundUnitDTO = new RefundUnitDTO();
        refundUnitDTO.setRefundOrderCode(refundQueryResp.getOutRefundNo());
        refundUnitDTO.setRefundId(refundQueryResp.getRefundId());
        refundUnitDTO.setRefundFee(refundQueryResp.getRefundFee());
        refundUnitDTO.setSettlementRefundFee(refundQueryResp.getSettlementRefundFee());
        refundUnitDTO.setRefundStatus(refundQueryResp.getRefundStatus());
        return refundUnitDTO;
    }


    public static WechatJsPayResponse wechatJsPayResponse(WechatPayJsRequest wechatPayJsRequest) {
        WechatJsPayResponse wechatJsPayResponse = new WechatJsPayResponse();
        wechatJsPayResponse.setAppId(wechatPayJsRequest.getAppId());
        wechatJsPayResponse.setNonceStr(wechatPayJsRequest.getNonceStr());
        wechatJsPayResponse.setPackageStr(wechatPayJsRequest.getPackageStr());
        wechatJsPayResponse.setPaySign(wechatPayJsRequest.getPaySign());
        wechatJsPayResponse.setTimeStamp(wechatPayJsRequest.getTimeStamp());
        wechatJsPayResponse.setSignType(wechatPayJsRequest.getSignType());
        return wechatJsPayResponse;
    }

    public static WechatAppPayResponse wechatAppPayResponse(WechatPayAppRequest wechatPayAppRequest) {
        WechatAppPayResponse wechatPayAppRequestDTO = new WechatAppPayResponse();
        wechatPayAppRequestDTO.setAppid(wechatPayAppRequest.getAppid());
        wechatPayAppRequestDTO.setNoncestr(wechatPayAppRequest.getNoncestr());
        wechatPayAppRequestDTO.setPackageStr(wechatPayAppRequest.getPackageStr());
        wechatPayAppRequestDTO.setPartnerid(wechatPayAppRequest.getPartnerid());
        wechatPayAppRequestDTO.setPrepayid(wechatPayAppRequest.getPrepayid());
        wechatPayAppRequestDTO.setSign(wechatPayAppRequest.getSign());
        wechatPayAppRequestDTO.setTimestamp(wechatPayAppRequest.getTimestamp());
        return wechatPayAppRequestDTO;
    }

    public static CustomerPaidOrderRespDTO customerPaidOrderRespDTO(List<PayRecord> payRecords) {
        CustomerPaidOrderRespDTO customerPaidOrderRespDTO = new CustomerPaidOrderRespDTO();
        customerPaidOrderRespDTO.setOrderCodes(Utils.qmap(payRecords, PayRecord::getOrderCode));
        return customerPaidOrderRespDTO;
    }
}
